package demo1;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    public static void bubbleSort(Student[] students, Comparator<Student> comparator) {//注意这里的写法,利用比较器重写了bubble
        for(int i=0;i< students.length-1;i++){
            for(int j=0;j< students.length-1-i;j++){
                if(comparator.compare(students[j],students[j+1])>0){
                    Student tmp=students[j];
                    students[j]=students[j+1];
                    students[j+1]=tmp;
                }
            }
        }
    }
    public static void bubbleSort(Comparable[] comparables) {//这个就是用Comparable接口重写的冒泡算法
        for(int i=0;i< comparables.length-1;i++){
            for(int j=0;j< comparables.length-1-i;j++){
                if(comparables[j].compareTo(comparables[j+1])>0){
                    Comparable tmp=comparables[j];
                    comparables[j+1]=comparables[j];
                    comparables[j]=tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("zz",1);
        students[1]=new Student("ww",2);
        students[2]=new Student("ee",3);
//        Arrays.sort(students);
//        bubbleSort(students);//利用Comparable接口来重写冒泡排序
//        System.out.println(Arrays.toString(students));
        AgeComparator ageComparator=new AgeComparator();
//        int ret=ageComparator.compare(students[0],students[1]);
//        if(ret>0){
//            System.out.println("年龄关系:s1>s2");
//        }else if(ret<0){
//            System.out.println("年龄关系:s1<s2");
//        }else{
//            System.out.println("年龄关系:s1=s2");
//        }
        bubbleSort(students,ageComparator);//利用比较器来实现重写bubble算法
        System.out.println(Arrays.toString(students));

    }
}
